import {
    ipcMain,
    Menu,
    BrowserWindow,
    BrowserView,
    ipcRenderer,
    shell,
    screen
} from "electron";
import {
    createProtocol
} from 'vue-cli-plugin-electron-builder/lib'
import * as path from "path"



let menuTemplate = [{
    label: "帮助",
    submenu: [{
            label: "在线帮助",
            click: () => {
                // 在外部浏览器中打开链接地址
                shell.openExternal("http://www.baidu.com");
            }
        },
        {
            label: "关于我们",
            click: () => {
                var url = "https://m.baidu.com/";
                // shell.openExternal("https://www.jianshu.com/u/e9a31a3c3bdb")
                // 传递消息给渲染进程index,在iframe中打开外部链接
                BrowserWindow.getFocusedWindow().webContents.send("openIframe", url)
            }
        }
    ]
}, {
    label: "打开控制台",
    click: () => {
        BrowserWindow.getFocusedWindow().webContents.openDevTools()

    }
}];


// 固定写法
var menuBuilder = Menu.buildFromTemplate(menuTemplate);
Menu.setApplicationMenu(menuBuilder);



const handleSetTitle = (event, title) => {
    const webContents = event.sender
    const win = BrowserWindow.fromWebContents(webContents)
    win.setTitle(title)
}



export default function ipcMainLoad() {

    ipcMain.on('set-title', handleSetTitle)

    ipcMain.handle('open-console', async (event, msg) => {
        const webContents = event.sender
        const mainWindow = BrowserWindow.fromWebContents(webContents)
        mainWindow.webContents.openDevTools()
    })

    ipcMain.handle('reload-window', async (event, msg) => {
        const webContents = event.sender
        const mainWindow = BrowserWindow.fromWebContents(webContents)
        mainWindow.webContents.reload();
    })



    ipcMain.handle('maximize-window', async (event, flag = true) => {
        const webContents = event.sender
        const mainWindow = BrowserWindow.fromWebContents(webContents)
        mainWindow.setFullScreen(flag);
    })

    ipcMain.handle('minimize-window', async (event, msg) => {
        const webContents = event.sender
        const mainWindow = BrowserWindow.fromWebContents(webContents)
        mainWindow.minimize();
    })



    ipcMain.handle('close-main-window', async (event, msg) => {
        const webContents = event.sender
        const mainWindow = BrowserWindow.fromWebContents(webContents)
        try {
            webContents && webContents.destroy && webContents.destroy();
            mainWindow.close();
        } catch (error) {

        }

    })

    ipcMain.handle('open-window', async (event, msg) => {
        console.log('msg', msg)
        let workAreaSize = screen.getPrimaryDisplay().workAreaSize
        let newWin;
        newWin = new BrowserWindow({
            width: 350,
            height: 650,
            x: workAreaSize.width - 380,
            y: 30,
            frame: false, //隐藏默认头部
            icon: path.join(__dirname, '../resources/images/logo.png'),
            webPreferences: {
                // 预加载脚本
                //  sandbox: false,
                //  preload: path.join(__dirname,  './preload.js'),
                webSecurity: false, // 取消跨域
                nodeIntegration: true, // v5版本开始需要加多这一行
                contextIsolation: false, // v12版本需要加多这一行
                enableRemoteModule: true //v10版本 打开remote模块
            }

        });
        newWin.setMenu(null)
        // process.env.WEBPACK_DEV_SERVER_URL+
        // newWin.loadURL("https://www.baidu.com/")

        if (process.env.WEBPACK_DEV_SERVER_URL) {
            // Load the url of the dev server if in development mode
            await newWin.loadURL(process.env.WEBPACK_DEV_SERVER_URL + '/#/child-window')
            if (!process.env.IS_TEST) newWin.webContents.openDevTools()
        } else {
            createProtocol('app')
            // Load the index.html when not in development
            newWin.loadURL('app://./index.html/#/child-window')
        }

        newWin.on("closed", () => {
            newWin = null;
        })


    })



    ipcMain.handle('open-browser', async (event, msg) => {
        const webContents = event.sender
        const mainWindow = BrowserWindow.fromWebContents(webContents)
        // 使用windowsView 打开子窗口
        let view = new BrowserView()
        view.setBounds({
            x: 0,
            y: 120,
            width: 800,
            height: 800
        })
        view.webContents.loadURL('https://www.jianshu.com/u/e9a31a3c3bdb')
        mainWindow.setBrowserView(view)
    })


    ipcMain.handle('close-window', async (event, msg) => {
        const webContents = event.sender
        const mainWindow = BrowserWindow.fromWebContents(webContents)
        mainWindow.destroy();
    })

    ipcMain.on('show-menu', (event) => {
        const template = [{

                label: '复制',
                // type: 'checkbox', checked: true 选中的菜单前面会有√
                type: 'checkbox',
                // checked: true,
                // 点击事件 调用方法 menu-method1 与 demo.js 中 ipcRenderer.on('menu-method1') 为对应关系
                click: () => {
                    console.log("复制")
                    event.sender.send('menu-method1', '123213')
                }

            },

            //  type: 'separator' 分割线 加上之后两个按钮之间有一条分割线
            {
                type: 'separator'
            },
            {
                label: '粘贴',
                type: 'checkbox',
                click: () => {
                    console.log("粘贴")
                }
            }
        ]
        const menu = Menu.buildFromTemplate(template)
        menu.popup(BrowserWindow.fromWebContents(event.sender))
    })


}